package com.yuan.cfycomment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.example.jhf.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 涓�绾ц瘎璁烘牱寮�
 * 
 * @author Administrator
 * 
 */
public class CommentAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private List<HashMap<String, String>> list_comment; // 涓�绾ц瘎璁烘暟鎹�

	public CommentAdapter(Context context,
			List<HashMap<String, String>> list_comment,
			List<List<HashMap<String, String>>> list_comment_child,
			OnClickListener myOnitemcListener) {
		this.inflater = LayoutInflater.from(context);
		this.list_comment = new ArrayList<HashMap<String, String>>();
		this.list_comment.addAll(list_comment);
	}

	public void clearList() {
		this.list_comment.clear();// 绉婚櫎杩欎釜鍒楄〃涓厓绱狅紝璁╀粬涓虹┖
	}

	/**
	 * 淇敼璇勮
	 * 
	 * @param list_comment
	 * @param list_comment_child
	 */
	public void updateList(List<HashMap<String, String>> list_comment,
			List<List<HashMap<String, String>>> list_comment_child) {
		this.list_comment.addAll(list_comment);
	}

	@Override
	public int getCount() {
		return list_comment.size();
	}

	@Override
	public Object getItem(int position) {
		return list_comment.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_comment, null);
			viewHolder = new ViewHolder();
			viewHolder.linearLayout = (LinearLayout) convertView
					.findViewById(R.id.school_linearyout);
			viewHolder.iv_user_photo = (ImageView) convertView
					.findViewById(R.id.iv_user_photo);
			viewHolder.tv_user_name = (TextView) convertView
					.findViewById(R.id.tv_user_name);

			viewHolder.tv_user_comment = (TextView) convertView
					.findViewById(R.id.tv_user_comment);
			viewHolder.tv_user_tiem_s = (TextView) convertView
					.findViewById(R.id.tv_user_tiem_s);
			viewHolder.tv_userHome_name = (TextView) convertView
					.findViewById(R.id.tv_userHome_name);
			viewHolder.tv_answer_school = (TextView) convertView
					.findViewById(R.id.tv_user_reply);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String strhome = list_comment.get(position).get("content").toString();
		String strschool = list_comment.get(position).get("schoolanswer");
		String studentportrait = list_comment.get(position).get(
				"studentportrait");
		// stringtoBitmap(studentportrait);
		viewHolder.iv_user_photo
				.setImageBitmap(stringtoBitmap(studentportrait));
		// 姝ｅ垯琛ㄨ揪寮忓幓鎺塰eml<p></P>
		String regEx = "</?[p|P][^>]*>";
		if (strschool != null) {
			Pattern pa = Pattern.compile(regEx);
			Matcher matcher = pa.matcher(strschool);
			String mschool = matcher.replaceAll("").trim();
			viewHolder.tv_answer_school.setText(mschool);
		} else {

		}
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(strhome);
		String rue = m.replaceAll("").trim();
		viewHolder.tv_user_name.setText(list_comment.get(position)
				.get("classNameStr").toString());
		viewHolder.tv_user_tiem_s.setText(list_comment.get(position)
				.get("TiemIssueStr").toString());
		viewHolder.tv_userHome_name.setText(list_comment.get(position)
				.get("homename").toString());
		viewHolder.tv_user_comment.setText(rue);

		if (list_comment.get(position).get("schoolanswer") != null) {
			viewHolder.linearLayout.setVisibility(View.VISIBLE);
		} else {
			viewHolder.linearLayout.setVisibility(View.GONE);
		}
		return convertView;
	}

	public class ViewHolder {
		private ImageView iv_user_photo; // 鍝佽鑰� 澶村儚
		private TextView tv_user_name; // 鍝佽鑰� 鏄电О
		private TextView tv_user_comment; // 鍝佽鑰� 涓�绾у搧璁哄唴瀹�
		private TextView tv_user_tiem_s;// 鍙戝竷鏃堕棿
		private TextView tv_userHome_name;// 瀹堕暱鍚嶅瓧
		private TextView tv_answer_school;// 瀛︽牎鍥炵瓟
		private LinearLayout linearLayout;
	}

	private Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}
}
